package com.example.crm01.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm01.entity.system.Equipment;
import com.example.crm01.mapper.system.EquipmentMapper;
import com.example.crm01.service.EquipmentService;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.system.EquipmentAddForm;
import com.example.crm01.service.form.system.EquipmentListForm;
import com.example.crm01.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {

    public final EquipmentMapper equipmentMapper;

    public EquipmentServiceImpl(EquipmentMapper equipmentMapper) {
        this.equipmentMapper = equipmentMapper;
    }

    @Override
    public Page<Equipment> listEquipment(EquipmentListForm form) {
        Page<Equipment> page = new Page<>(form.getPageNum(), form.getPageSize());

        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(form.getEquipmentType())) {
            queryWrapper.like(Equipment::getEquipmentType, form.getEquipmentType());
        }
        if (StringUtils.isNotEmpty(form.getEquipmentModel())) {
            queryWrapper.like(Equipment::getEquipmentModel, form.getEquipmentModel());
        }
        if (StringUtils.isNotEmpty(form.getEquipmentManufacturers())) {
            queryWrapper.like(Equipment::getEquipmentManufacturers, form.getEquipmentManufacturers());
        }

        if (StringUtils.isNotEmpty(form.getRegionName()) && ObjectUtils.isNotEmpty(form.getRegionId())) {
            queryWrapper.eq(Equipment::getRegionName, form.getRegionName());
            queryWrapper.eq(Equipment::getRegionId, form.getRegionId());
        }

        queryWrapper.ne(Equipment::getDel_flag, 2);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean addEquipment(EquipmentAddForm form) {
        Equipment equipment = new Equipment();
        equipment.setEquipmentType(form.getEquipmentType());
        equipment.setEquipmentModel(form.getEquipmentModel());
        equipment.setEquipmentManufacturers(form.getEquipmentManufacturers());
        equipment.setRegionId(form.getRegionId());
        equipment.setRegionName(form.getRegionName());
        equipment.setEquipmentNum(form.getEquipmentNum());

        //-----
        equipment.setCreateBy("1");
        equipment.setCreateTime(LocalDateTime.now());
        equipment.setUpdateBy("1");
        equipment.setUpdateTime(LocalDateTime.now());
        equipment.setDel_flag(0);
        int insert = equipmentMapper.insert(equipment);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateEquipment(EquipmentAddForm form) {
        if (ObjectUtils.isEmpty(form.getId())) {
            throw new RuntimeException("修改设备信息，主键id不能为空");
        }
        Equipment equipment = baseMapper.selectById(form.getId());
        equipment.setEquipmentType(form.getEquipmentType());
        equipment.setEquipmentModel(form.getEquipmentModel());
        equipment.setEquipmentManufacturers(form.getEquipmentManufacturers());
        equipment.setRegionId(form.getRegionId());
        equipment.setRegionName(form.getRegionName());
        equipment.setEquipmentNum(form.getEquipmentNum());

        //-----
        equipment.setUpdateBy("1");
        equipment.setUpdateTime(LocalDateTime.now());
        equipment.setDel_flag(0);
        int insert = equipmentMapper.insert(equipment);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteEquipment(BaseIdForm form) {
        Equipment equipment = baseMapper.selectById(form.getId());
        if (ObjectUtils.isEmpty(equipment)) {
            throw new RuntimeException("设备信息不存在删除失败");
        }
        equipment.setUpdateBy("1");
        equipment.setUpdateTime(LocalDateTime.now());
        equipment.setDel_flag(2);
        int update = baseMapper.updateById(equipment);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
